/*
 * @Author: Aaron
 * @Date: 2021-08-25 17:36:04
 * @LastEditors: Aaron
 * @LastEditTime: 2022-04-27 18:05:48
 * @Description: file content
 * @FilePath: \ias_ui\src\utils\item.js
 * @Company: 沃尔玛
 */
import { doDownload } from '@/api/download'
import { Message } from 'element-ui'
import { authentication } from '@/utils/processingData.js'

// 下载新增页面文件
export function exportTempDataExcel(tempData, changeType) {
  if (!tempData || tempData.length < 1) {
    Message({
      message: '无数据可进行导出。',
      type: 'error',
      duration: 3 * 1000
    })
  } else {
    this.$confirm('确认导出该批临时数据?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.loading = true
      doDownload('/change-item/exportTempDataExcel', { tempData: tempData, changeType: changeType }, getFileNameByType(changeType)).then(() => {
        this.loading = false
        this.dialog = false
      }, error => {
        console.log(error)
        this.loading = false
      })
    })
  }
}

// 查看附件
export function previewFile(filePath) {
  location.href = authentication(filePath)
}

export function getFileNameByType(changeType) {
  switch (changeType) {
    case 1:
      return 'item_maintenance.xlsx'
    case 2:
      return 'ixiu.xlsx'
    case 3:
      return 'traits_maintenance.xlsx'
    case 4:
      return 'plu_maintenance.xlsx'
    case 6:
      return 'deletion.xlsx'
    default:
      return ''
  }
}

// 拖动表头改变列宽
export function colChange(tabId) {
  setTimeout(() => {
    var applyTableColWidths = []
    var applyTable = document.getElementById(tabId)
    var applyTableColgroup = applyTable.getElementsByTagName('colgroup')[0]
    var applyTableCol = applyTableColgroup.getElementsByTagName('col')
    for (var i = 0; i < applyTableCol.length; i++) {
      console.log(applyTableCol[i])
      applyTableColWidths.push(applyTableCol[i].width)
    }
    localStorage.setItem(tabId, JSON.stringify(applyTableColWidths))
  }, 500)
}

// 获取浏览器缓存的列宽
export function getTableColWidth(tabId) {
  var tableWidth = localStorage.getItem(tabId)
  if (tableWidth) {
    var _tableWidth = JSON.parse(tableWidth)
    console.log(_tableWidth)
    var applyTable = document.getElementById(tabId)
    var applyTableColgroup = applyTable.getElementsByTagName('colgroup')
    for (var i = 0; i < applyTableColgroup.length; i++) {
      var applyTableCol = applyTableColgroup[i].getElementsByTagName('col')
      for (var j = 0; j < applyTableCol.length; j++) {
        console.log(applyTableCol[j].style.width)
        applyTableCol[j].style.width = _tableWidth[j]
        console.log(applyTableCol[j].style.width)
        console.log(applyTableCol[j])
      }
    }
  }
}

// 下载Excel模板
export function downloadExcelTemplate(changeType, templateType) {
  var url = process.env.VUE_APP_BASE_API + '/exportTemplate/downloadExcelTemplate?changeType=' + changeType + '&templateType=' + templateType
  window.open(authentication(url))
}
